#include "WVFile.h"
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>

WVFile::WVFile(size_t capacity)
{
	wvectors_.reserve(capacity);
}

WVFile::~WVFile()
{
}

bool WVFile::Read(const char* filepath)
{
	using namespace std;

	ifstream is;
	is.open(filepath);
	if (!is.is_open())
	{
		return false;
	}

	string line;
	vector<string> tokens;
	float data[WVector::DIM];

	while (!is.eof())
	{
		std::getline(is, line);
		if (line.empty())
		{
			continue;
		}

		// split string to vector.
		tokens.clear(); 
		istringstream iss(line);		
		copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter(tokens));

		if (tokens.size() != WVector::DIM + 1)
		{
			printf("Invalid token size %d\n.", tokens.size());
			return false;
		}

		for (size_t i = 0; i < WVector::DIM; ++i)
		{
			data[i] = (float)atof(tokens[i + 1].c_str());
		}

		WVector vector(tokens[0], data);
		wvectors_.push_back(vector);
	}

	is.close();

	printf("Num words = %d\n", wvectors_.size());

	return true;
}
